package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;

import model.Rang;

import service.Service;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DialogboxSpiller extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txfNavn;
	private JComboBox comboBox;
	private DefaultComboBoxModel cBoxModel = new DefaultComboBoxModel();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DialogboxSpiller dialog = new DialogboxSpiller();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DialogboxSpiller() {
		setTitle("Opret spiller");
		setBounds(500, 100, 190, 167);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			FlowLayout flowLayout = (FlowLayout) panel.getLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			contentPanel.add(panel, BorderLayout.NORTH);
			{
				JLabel lblNavn = new JLabel("Navn:");
				panel.add(lblNavn);
			}
			{
				txfNavn = new JTextField();
				panel.add(txfNavn);
				txfNavn.setColumns(10);
			}
		}
		{
			JPanel panel = new JPanel();
			FlowLayout flowLayout = (FlowLayout) panel.getLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			contentPanel.add(panel, BorderLayout.CENTER);
			{
				JLabel lblRang = new JLabel("Rang:");
				panel.add(lblRang);
			}
			{
				comboBox = new JComboBox();
				cBoxModel.addElement(Rang.NOOB);
				cBoxModel.addElement(Rang.GOOD);
				cBoxModel.addElement(Rang.AWESOME);
				comboBox.setModel(cBoxModel);
				panel.add(comboBox);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new OkButtonActionListener());
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new CancelButtonActionListener());
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	private class OkButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent ae) {
			try{
				if(!txfNavn.getText().isEmpty()){
				Service.getInstance().opretSpiller(txfNavn.getText(), (Rang)comboBox.getSelectedItem());
				txfNavn.setText("");
				dispose();
				}
				else{
					throw new Guiexception("Husk at skrive et navn!");
				}
			}
			catch(Guiexception e){
				JOptionPane.showMessageDialog(null, e.getMessage(), "Fejl", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	private class CancelButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			txfNavn.setText("");
			dispose();
		}
	}
}
